<template>
  <el-dialog
    :close-on-click-modal="false"
    title="重大危险源清单"
    :visible.sync="show"
    width="1000px"
    center
  >
    <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="130px">
      <div class="com-form-title">基础信息</div>
      <el-row>
        <el-col :span="8">
          <el-form-item label="机构名称" prop="organizationName">
            <el-input v-model="form.organizationName" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="编号" prop="number">
            <el-input v-model="form.number" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="重大危险源名称" prop="sourceName">
            <el-input v-model="form.sourceName" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="危险工序，作业活动" prop="active" label-width="150px">
            <el-input v-model="form.active" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="行业" prop="industry">
            <el-select v-model="form.industry" :disabled="disableForm" style="width: 100%;" placeholder="请选择行业">
              <el-option v-for="item in riskSourceChecklistIndustry" :key="item.name" :label="item.name" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="重大危险源级别" prop="sourceLevel">
            <el-input v-model="form.sourceLevel" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="具体部位或位置" prop="location">
            <el-input v-model="form.location" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="危险源描述" prop="sourceDesc">
            <el-input v-model="form.sourceDesc" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="危害事件" prop="event">
            <el-input v-model="form.event" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="情况" prop="condType">
            <el-select v-model="form.condType" :disabled="disableForm" style="width: 100%;" placeholder="请选择情况">
              <el-option v-for="item in riskSourceChecklistCondType" :key="item.name" :label="item.name" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="有无管理措施" prop="measureHas">
            <el-select v-model="form.measureHas" :disabled="disableForm" style="width: 100%;" placeholder="请选择有无管理措施">
              <el-option v-for="item in riskSourceChecklistMeasureHas" :key="item.name" :label="item.name" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="有无指定管理方案" prop="plan">
            <el-select v-model="form.plan" :disabled="disableForm" style="width: 100%;" placeholder="请选择有无指定管理方案">
              <el-option v-for="item in riskSourceChecklistPlan" :key="item.name" :label="item.name" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="主要措施" prop="measureDesc">
            <el-input v-model="form.measureDesc" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="项目是否受控" prop="projectControlled">
            <el-select v-model="form.projectControlled" :disabled="disableForm" style="width: 100%;" placeholder="请选择项目是否受控">
              <el-option v-for="item in riskSourceChecklistProjectControlled" :key="item.name" :label="item.name" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="项目负责人" prop="projectHead">
            <el-input v-model="form.projectHead" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="监管部门" prop="monitorDepartment">
            <el-input v-model="form.monitorDepartment" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="监管负责人" prop="monitorHead">
            <el-input v-model="form.monitorHead" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="监管层级" prop="monitorLevel">
            <el-input v-model="form.monitorLevel" :disabled="disableForm" />
          </el-form-item>
        </el-col>
      </el-row>
      <div class="com-form-title">附件</div>
      <!-- <el-form-item label="文件上传">
        <uploader v-model="fileList" :disabled="disableForm" :can-download="optType==='detail'" />
      </el-form-item> -->
      <div>
        <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
      </div>
      <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="saveDoc">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addRiskSourceChecklist, updateRiskSourceChecklist, getRiskSourceChecklistDetail } from '@/api/securityManage'
import { getAreaList } from '@/api/common'
import { riskSourceChecklistIndustry, riskSourceChecklistCondType, riskSourceChecklistMeasureHas, riskSourceChecklistPlan, riskSourceChecklistProjectControlled } from '@/utils/dicData'
import fileUploader from '@/components/FileUploader'

export default {
  name: 'AddRiskSourceChecklist',
  components: {
    fileUploader
  },
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    optType: {
      type: String,
      default: 'add'
    },
    dir: {
      type: Object,
      default() {
        return {}
      }
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      riskSourceChecklistIndustry,
      riskSourceChecklistCondType,
      riskSourceChecklistMeasureHas,
      riskSourceChecklistPlan,
      riskSourceChecklistProjectControlled,
      showFileModal: false,
      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        organizationName: '',
        number: '',
        sourceName: '',
        sourceLevel: '',
        sourceDesc: '',
        industry: '',
        location: '',
        event: '',
        condType: '',
        measureHas: '',
        measureDesc: '',
        active: '',
        plan: '',
        projectControlled: '',
        projectHead: '',
        monitorDepartment: '',
        monitorHead: '',
        monitorLevel: '',
        createTime: ''
      },
      fileList: [
      ],
      rules: {
        name: [
          { required: true, message: '单位名称', trigger: 'change' }
        ],
        unifiedSocialCreditCode: [
          { required: true, message: '统一社会信用代码', trigger: 'change' }
        ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    }
  },
  watch: {
    showModal(val) {
      if (val && this.optType !== 'add') {
        // this.getDocDetail(this.data.id)
        this.$nextTick(() => {
          this.getDocDetail(this.data.id)
          Object.keys(this.form).forEach(key => {
            const source = this.data[key]
            if (typeof source === 'boolean') {
              this.form[key] = source ? 1 : 0
            } else {
              this.form[key] = source
            }
          })
        })
      } else if (!val) {
        this.fileList = []
        this.$refs.form.resetFields()
      }
    }
  },
  mounted() {
    this.getRegionData()
  },
  methods: {
    provinceChange(e) {
      const selectedData = this.provinceData.filter(val => val.name === e)[0]
      if (selectedData) {
        this.form.city = ''
        const params = {
          parentCode: selectedData.code,
          areaType: 2
        }
        getAreaList(params).then(res => {
          this.cityData = res.data || []
        })
      }
    },
    getDocDetail(id) {
      getRiskSourceChecklistDetail({ riskSourceChecklistId: id }).then(res => {
        const { lstAttachment } = res.data || {}
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
      })
    },
    getRegionData() {
      getAreaList({ areaType: '1' }).then(res => {
        this.provinceData = res.data || []
      })
    },
    saveDoc() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const params = {
            riskSourceChecklist: {
              ...this.form,
              ...this.optType === 'edit' ? {
                id: this.data.id
              } : {}
            },
            lstAttachment: this.fileList
          }
          const fn = this.optType === 'add' ? addRiskSourceChecklist : updateRiskSourceChecklist
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
</style>
